﻿<%@ WebHandler Language="C#" Class="control" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Collections.Specialized;

public class control : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        NameValueCollection queryString = context.Request.QueryString;
        DelayWeiboEnum type = (DelayWeiboEnum)Convert.ToInt32(queryString["type"]);
        int id = Convert.ToInt32(queryString["id"]);
        string weibId =queryString["weiboId"];
        string reply =queryString["reply"];
        string replyAccount =queryString["replyAccount"];
        string commentId =queryString["commentId"];
        DateTime replyTime = TimeUtil.ToDateTime(Convert.ToInt64(queryString["replyTime"]));//new DateTime(1970, 1, 1).Add(new TimeSpan(Convert.ToInt64(queryString["replyTime"]) * 1000 * 10));
        DelayWeiboProxy proxy = new DelayWeiboProxy();

        switch (type)
        {
            case DelayWeiboEnum.ADD:
                object data = proxy.Add(weibId, reply, replyAccount, replyTime, commentId);
                if (data is string)
                    context.Response.Write(Message.CreateMessage(false, data as string));
                else
                    context.Response.Write(Message.CreateMessage(true, (data as DelayWeiboData).toJson()));
                break;
            case DelayWeiboEnum.DELETE:
                context.Response.Write(Delete(id));
                break;
            case DelayWeiboEnum.SELECT_NEW_WEIBO_LIST:
                context.Response.Write(SelectDelayWeiboList());
                break;
            case DelayWeiboEnum.UPDATE:
                context.Response.Write(Update(id, reply));
                break;
        }
    }

    private string Update(int id, string reply)
    {
        DelayWeiboProxy proxy = new DelayWeiboProxy();
        DelayWeiboData data = proxy.Update(id, reply);
        if (data != null)
            return Message.CreateMessage(true, data.toJson());
        else
            return Message.CreateMessage(false, "定时微博不存在");
    }

    private string Delete(int id)
    {
        DelayWeiboProxy proxy = new DelayWeiboProxy();
        DelayWeiboData data = proxy.Delete(id);
        if (data != null)
            return Message.CreateMessage(true, data.toJson());
        else 
            return Message.CreateMessage(false, "定时微博已删除");
    }
    
    private string SelectDelayWeiboList()
    {
        List<DelayWeiboData> list = new DelayWeiboProxy().SelectDelayList();
        if (list == null)
            return Message.CreateMessage(true, "[]");
        else
        {
            string result = "";
            for (int i = 0; i < list.Count; i++)
            {
                result += list[i].toJson();
                if (i < list.Count - 1)
                    result += ",";
            }
            return Message.CreateMessage(true, "[" + result + "]");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}